package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.text.TextUtils;

import com.shuqi.model.bean.MyFavoritBean;
import com.shuqi.model.bean.MyFavoritBean.MyFavorityItemBean;
import com.shuqi.model.parse.HandlerBase;

/**
 * 我的收藏网络访问解析器
 * 
 * @author li.li
 * @date 2014-06-11
 * 
 */
public class MyFavoritListParser extends HandlerBase {
    private List<MyFavorityItemBean> list;
    private MyFavorityItemBean myFavorityItemBean;
    private MyFavoritBean myFavoritBean;
    private String preTag;

    @Override
    public Object getParsedData() {
        return myFavoritBean;
    }

    @Override
    public void startDocument() throws SAXException {
        myFavoritBean = new MyFavoritBean();
        list = new ArrayList<MyFavorityItemBean>();
        super.startDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {

        if ("ColBook".equals(qName)) {
            myFavorityItemBean = new MyFavorityItemBean();
            myFavorityItemBean.setBookId(attributes.getValue("bookId"));
            myFavorityItemBean.setBookName(attributes.getValue("bookName"));
            myFavorityItemBean.setAuthor(attributes.getValue("author"));
            myFavorityItemBean.setColTime(attributes.getValue("colTime"));
            myFavorityItemBean.setHide(attributes.getValue("hide"));
            myFavorityItemBean.setImgUrl(attributes.getValue("imgUrl"));
            myFavorityItemBean.setSqCoverIsopen(attributes.getValue("sq_cover_isopen"));
            myFavorityItemBean.setSrCoverIsopen(attributes.getValue("sr_cover_isopen"));
            myFavorityItemBean.setCrCoverIsopen(attributes.getValue("cr_cover_isopen"));
            myFavorityItemBean.setSqReadIsopen(attributes.getValue("sq_read_isopen"));
            myFavorityItemBean.setSrReadIsOpen(attributes.getValue("sr_read_isopen"));
            myFavorityItemBean.setCrReadIsOpen(attributes.getValue("cr_read_isopen"));
        }
        
        if ("Collections".equals(qName)) {
            myFavoritBean.setState(attributes.getValue("state"));
            myFavoritBean.setMessage(attributes.getValue("message"));
        }

        preTag = qName;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {}

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("ColBook".equals(qName) && !TextUtils.isEmpty(myFavorityItemBean.getBookId())) {
            list.add(myFavorityItemBean);
            myFavorityItemBean = null;
        }

        preTag = null;
    }



    @Override
    public void endDocument() throws SAXException {
        if (list != null && !list.isEmpty()) {
            myFavoritBean.setItemList(list);
//            myFavoritBean.setState("304");
//            myFavoritBean.setMessage("身份验证失败测试");
        }
    }

}
